﻿using System;
using HDM.NexusEngine.Core;
using HDM.NexusEngine.Input;
using Tao.OpenGl;

namespace HDM.NexusEngine.Shooter.GameStates
{
    public class StartMenuState : IGameObject
    {
        private Renderer _renderer = new Renderer();
        private Text _title;
        private Font _generalFont;
        private InputManager _input;
        private VerticalMenu _menu;
        private StateSystem _system;

        public StartMenuState(Font titleFont, Font generalFont, InputManager input, StateSystem system)
        {
            _input = input;
            _generalFont = generalFont;
            _system = system;
            _title = new Text("Shooter", titleFont);
            _title.SetColor(new Color(0, 0, 0, 1));
            // Center on the x and place somewhere near the top.
            _title.SetPosition(-_title.Width/2, 300);

            InitializeMenu();
        }

        public void Update(double elapsedTime)
        {
            _menu.HandleInput();
        }

        public void Render()
        {
            Gl.glClearColor(1, 1, 1, 0);
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
            _renderer.DrawText(_title);
            _menu.Render(_renderer);
            _renderer.Render();
        }

        private void InitializeMenu()
        {
            _menu = new VerticalMenu(0, 150, _input);

            var startGame = new Button(
                delegate(object o, EventArgs e)
                {
                    // Do start game functionality.
                    _system.ChangeState("inner_game");
                },
                new Text("Start", _generalFont)
            );

            var exitButton = new Button(
                delegate(object o, EventArgs e)
                {
                    // Quit.
                    System.Windows.Forms.Application.Exit();
                },
                new Text("Exit", _generalFont)
            );

            _menu.AddButton(startGame);
            _menu.AddButton(exitButton);
        }
    }
}